// index.js
// 获取应用实例
import env from "../../utils/env";
const app = getApp()
Page({
  data: {
    ToDoList: [],
    nickName:'游客',
    userInfo:[
      {
        // icon: "/image/class.png",
        title: "历史记录",
        // littleTitle: "历史记录",
        click: "class"
      }
    ]
  },
  onLoad() {
    this.checkLogin(res => {
        console.log('is_login:', res.is_login);
        if (!res.is_login) {
          this.login();
        }else{
            this.setData({
                nickName: wx.getStorageSync('third_session').substring(0,10)
            });
            this.getHistoryInfo();
        }
      })
  },
  globalData: {
    token: null //保存token
  },
  //检测是否已经登录
  checkLogin: function (callback) {
    var token = this.globalData.token
    if (!token) {
      //从数据缓存中获取token
      token = wx.getStorageSync('token')
      if (token) {
        this.globalData.token = token;
      } else {
        callback({
          is_login: false
        })
        return
      }
    }
    wx.request({
      url: env.api_host+ '/checklogin',
      data: {
        token: token
      },
      success: res => {
        callback({
          is_login: res.data.is_login
        })
      }
    })
  },
  class(e){
    console.log(e,"班级");
  },
  login(e){
    //调用登录
    let that = this;
    wx.login({
        success: function(res) {
         console.log(res);
          if (res.code) {
            //发起网络请求
            console.log('url:'+env.api_host+ '/miniLogin');
            wx.request({
                url: env.api_host+ '/miniLogin',
                data: {
                  code: res.code
                },
                method: 'POST',
                header: {'content-type': 'application/json'},
                success: function(data){
                    if(data.data.code == 200){
                        wx.setStorageSync('isLogin', true);
                        wx.setStorageSync('third_session', data.data.third_session);
                        that.setData({
                            nickName: data.data.third_session.substring(0,10)
                        });
                        that.getHistoryInfo();
                    }
                },
                fail: function(data){
                },
                complete:function(data){
                    console.log(data);
                }
              })
          } else {
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        },
        fail: function(){
        console.log("启用wx.login函数，失败！");
        },
        complete:function(){
        console.log("已启用wx.login函数");
        }
    })
  },
  getHistoryInfo: function(){
      let req={
        remark: wx.getStorageSync('third_session')
      }
      console.log();
      var that = this;
    wx.request({
        url: env.api_host+ '/miniUser/records/list',
        method: 'get',
        header: {
            "content-type": "application/json"
        },
        data: req,
        success(res){
            console.log("length:", res.data.length);
            for(var i=0;i<res.data.length;i++){
                var newarray = {
                    createTime:res.data[i].createTime,
                    answer:res.data[i].answer,
                };        
                that.data.ToDoList = that.data.ToDoList.concat(newarray);
                that.setData({
                    ToDoList: that.data.ToDoList
                });
            }
        }
    })
  }
})
